#########################################################################################
# Package       Wx::Perl::SimpleThread::Handler::Event
# Description:  Wx Event style handler
# Created       Tue Mar 30 05:15:20 2010
# SVN Id        $Id: Event.pm 5 2010-03-30 09:55:07Z mark.dootson $
# Copyright:    Copyright (c) 2010 Mark Dootson
# Licence:      This program is free software; you can redistribute it 
#               and/or modify it under the same terms as Perl itself
#########################################################################################

package Wx::Perl::SimpleThread::Handler::Event;

#########################################################################################

use strict;
use warnings;
use Wx::Perl::SimpleThread::Handler;
use Wx;
use Wx::Event;
use base qw( Wx::Perl::SimpleThread::Handler );

our $VERSION = '0.02';

our $ID_CMD_EXIT     = Wx::NewEventType();
our $ID_CMD_DATA     = Wx::NewEventType();
our $ID_CMD_COMPLETE = Wx::NewEventType();

sub wxpEVT_SIMPLE_THREAD_EXIT               () { $ID_CMD_EXIT }
sub wxpEVT_SIMPLE_THREAD_DATA               () { $ID_CMD_DATA }
sub wxpEVT_SIMPLE_THREAD_REQUEST_COMPLETE   () { $ID_CMD_COMPLETE }

sub EVT_WXP_SIMPLE_THREAD_DATA              ($$) { $_[0]->Connect(-1,-1,&wxpEVT_SIMPLE_THREAD_DATA, $_[1] ) };
sub EVT_WXP_SIMPLE_THREAD_REQUEST_COMPLETE  ($$) { $_[0]->Connect(-1,-1,&wxpEVT_SIMPLE_THREAD_REQUEST_COMPLETE, $_[1] ) };
sub EVT_WXP_SIMPLE_THREAD_EXIT              ($$) { $_[0]->Connect(-1,-1,&wxpEVT_SIMPLE_THREAD_EXIT, $_[1] ) };

our @EXPORT = ();
our @EXPORT_OK = qw(
    EVT_WXP_SIMPLE_THREAD_DATA
    EVT_WXP_SIMPLE_THREAD_REQUEST_COMPLETE
    EVT_WXP_SIMPLE_THREAD_EXIT
    wxpEVT_SIMPLE_THREAD_EXIT
    wxpEVT_SIMPLE_THREAD_DATA
    wxpEVT_SIMPLE_THREAD_REQUEST_COMPLETE
);

our %EXPORT_TAGS = ( all => \@EXPORT_OK, everything => \@EXPORT_OK );

sub new {
    my ($class, $threadmodule, $eventhandler) = @_;
    my $self = $class->SUPER::new($threadmodule);
    return $self if !$self;
    $self->{_windowid} = $eventhandler->GetId;
    $self->{_wxid} = Wx::NewId();
    return $self;
}

sub on_thread_data_event {
    my($self, $data) = @_;
    if(my $handler = Wx::Window::FindWindowById($self->{_windowid}, undef)) {
        my $event = Wx::Perl::SimpleThread::Handler::Event::Event->new($ID_CMD_DATA, $self->{_wxid});
        $event->SetInt($self->tid);
        $event->SetClientData($data);
        $handler->AddPendingEvent($event);
    }
}

sub on_thread_request_complete {
    my($self, $data) = @_;
    if(my $handler = Wx::Window::FindWindowById($self->{_windowid}, undef)) {
        my $event = Wx::Perl::SimpleThread::Handler::Event::Event->new($ID_CMD_COMPLETE, $self->{_wxid});
        $event->SetClientData($data);
        $event->SetInt($self->tid);
        $handler->AddPendingEvent($event);
    }
}

sub on_thread_exit_event {
    my($self, $data) = @_;
    if(my $handler = Wx::Window::FindWindowById($self->{_windowid}, undef)) {
        my $event = Wx::Perl::SimpleThread::Handler::Event::Event->new($ID_CMD_EXIT, $self->{_wxid});
        $data = 'NULL';
        $event->SetClientData($data);
        $event->SetInt($self->tid);
        $handler->AddPendingEvent($event);
    }
}

package Wx::Perl::SimpleThread::Handler::Event::Event;
use strict;
use Wx;
use base qw( Wx::PlCommandEvent );

our $VERSION = '0.02';

sub new {
    my( $class, $type, $id ) = @_;
    my $self = $class->SUPER::new( $type, $id );
    return $self;
}

sub GetThreadId { $_[0]->GetInt; }
sub SetThreadId { $_[0]->SetInt( $_[1] ); }

sub Clone {
    my $self = shift;
    my $class = ref $self;
    my $clone = $class->new( $self->GetEventType(), $self->GetId() );
    $clone->SetInt( $self->GetInt );
    $clone->SetClientData( $self->GetClientData );
    return $clone;
}

1;
